using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClickSocial.Framework;
using System.IO;
using ClickSocial.Entities;
using ClickSocial.Components;
using System.Net.Mail;

namespace ClickSocial.Admin.controles
{
    public partial class NewUserEvent : NewUser
    {
        public new event EventHandler UserCreated;
        
        protected override void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                //guardo usuario
                DataSourceControls.UserDataSource.Insert(UserName.Text, (chkIsApproved.Visible == false ? true : chkIsApproved.Checked), Email.Text, Question.Text, Password.Text, Answer.Text);
                //guardo perfil
                PerfilComun perfil = (PerfilComun)ProfileBase.Create(UserName.Text);
                perfil.Nombre = txtNombre.Text;
                perfil.Apellido = txtApellido.Text;
                //perfil.Telefono = txtTelefono.Text;
                //perfil.Celular = txtCelular.Text;
                //perfil.Direccion = txtDireccion.Text;
                //perfil.Ciudad = txtCiudad.Text;
                //perfil.Provincia = txtPcia.Text;
                //perfil.Pais = txtPais.Text;

                if (ddlRoles.SelectedValue == "Fotografo")
                {
                    //Crear directorio de usuario
                    string BaseDirUser = string.Format("{0}\\{1}", Application["BaseDir"], UserName.Text);
                    if (Directory.CreateDirectory(BaseDirUser) == null) throw new ApplicationException("No se ha podido crear el directorio home del fotografo.");

                    perfil.UsaPrecio = chkPrecio.Checked;
                    long res = 0;

                    if (long.TryParse(txtEspacio.Text, out res))
                    {
                        res = res * 1024 * 1024;
                    }
                    else
                    {
                        res = 50 * 1024 * 1024;
                    }
                    perfil.Espacio = res;

                }
                perfil.Save();

                //guardo rol
                if (ddlRoles.Visible == true)
                {
                    Roles.AddUserToRole(UserName.Text, ddlRoles.SelectedValue);
                }

                UserCreated(sender, e);

            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }

        }

        public void Notificar(long eventId)
        {
            try
            {
                EventoInfo oEvento = Evento.GetOne(eventId);
                if (oEvento != null)
                {
                    Evento.AddUserEvent(eventId, UserName.Text);

                    MailMessage correo = new MailMessage();

                    MembershipUser user_actual = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                    correo.To.Add(user_actual.Email);
                    correo.Subject = "ClickSocial Notificacion";
                    correo.Body += string.Format("Evento: {0} \r\n", oEvento.Nombre);
                    correo.Body += string.Format("User: {0} \r\n", UserName.Text);
                    correo.Body += string.Format("Pass: {0} \r\n", Password.Text);
                    correo.IsBodyHtml = false;
                    correo.Priority = System.Net.Mail.MailPriority.Normal;

                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                    smtp.Send(correo);
                }
            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }
           
        }
    }
}